#!/usr/bin/perl

$flag_reverse = 0;

if ( scalar @ARGV > 0 ){
    if ( $ARGV[0] eq "-r" ) { $flag_reverse = 1 }
}

if ( not $flag_reverse ) {
    for (<STDIN>)
    {
        if ( not /^0x([0123456789abcdefABCDEF]+)\/(\d+)\s+(.*)/ ) { print; next }
        $x = sprintf "%08s\n", $1;
        $mask = $2;
        $rest = $3;
        $x =~ /(..)(..)(..)(..)/;
        printf "%-19s %s\n" , (sprintf "%s.%s.%s.%s/%s", hex($1), hex($2), hex($3), hex($4), $mask) , $rest;
    }
}

else { # reverse, dec to hex
    
    for (<STDIN>)
    {
        if ( not /^(\d+)\.(\d+)\.(\d+)\.(\d+)\/(\d+)\s+(.*)/ ) { print; next }
        $x = sprintf "%02x%02x%02x%02x" , $1,$2,$3,$4;
        $mask = $5;
        $rest = $6;
        $x =~ s/^0+//;
        if ( $x eq "" ) { $x = "0" }
        printf "0x%s/%s %s\n", $x,$mask,$rest;
    }
}

